package cn.iocoder.yudao.module.erp.controller.admin.stocksmoveitem;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.stocksmoveitem.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.stocksmoveitem.StocksMoveItemDO;
import cn.iocoder.yudao.module.erp.service.stocksmoveitem.StocksMoveItemService;

@Tag(name = "管理后台 - ERP 库存调拨项")
@RestController
@RequestMapping("/erp/stocks-move-item")
@Validated
public class StocksMoveItemController {

    @Resource
    private StocksMoveItemService stocksMoveItemService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 库存调拨项")
    @PreAuthorize("@ss.hasPermission('erp:stocks-move-item:create')")
    public CommonResult<Long> createStocksMoveItem(@Valid @RequestBody StocksMoveItemSaveReqVO createReqVO) {
        return success(stocksMoveItemService.createStocksMoveItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 库存调拨项")
    @PreAuthorize("@ss.hasPermission('erp:stocks-move-item:update')")
    public CommonResult<Boolean> updateStocksMoveItem(@Valid @RequestBody StocksMoveItemSaveReqVO updateReqVO) {
        stocksMoveItemService.updateStocksMoveItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 库存调拨项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:stocks-move-item:delete')")
    public CommonResult<Boolean> deleteStocksMoveItem(@RequestParam("id") Long id) {
        stocksMoveItemService.deleteStocksMoveItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 库存调拨项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:stocks-move-item:query')")
    public CommonResult<StocksMoveItemRespVO> getStocksMoveItem(@RequestParam("id") Long id) {
        StocksMoveItemDO stocksMoveItem = stocksMoveItemService.getStocksMoveItem(id);
        return success(BeanUtils.toBean(stocksMoveItem, StocksMoveItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 库存调拨项分页")
    @PreAuthorize("@ss.hasPermission('erp:stocks-move-item:query')")
    public CommonResult<PageResult<StocksMoveItemRespVO>> getStocksMoveItemPage(@Valid StocksMoveItemPageReqVO pageReqVO) {
        PageResult<StocksMoveItemDO> pageResult = stocksMoveItemService.getStocksMoveItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StocksMoveItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 库存调拨项 Excel")
    @PreAuthorize("@ss.hasPermission('erp:stocks-move-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStocksMoveItemExcel(@Valid StocksMoveItemPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StocksMoveItemDO> list = stocksMoveItemService.getStocksMoveItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 库存调拨项.xls", "数据", StocksMoveItemRespVO.class,
                BeanUtils.toBean(list, StocksMoveItemRespVO.class));
    }

}